<?php 

/*
* StiPHPcms, A web Content management System made with PHP/MySQL
* JordSti : jord52@gmail.com
* Version : 0.0.2
*
* Copyright (C) 2009  jord52@gmail.com
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2
* of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
*/

class FeedEntry {
	public $link;
	public $title;
	public $author;
	public $updated;
}

class AtomFeed {
	private $url;
	private $xml;
	public $entry;
	public $name;
	
	public function __construct($url,$name = 'no name',$useCache = false)
	{
		$this->url = $url;
		$this->name = $name;
		$this->xml = '';
		$this->entry = array();
		
		if($useCache)
		{
			if(!is_dir('cache/atom'))
			{
				mkdir('cache/atom',0777);
				$fp = fopen('cache/atom/index.php','w');
				fwrite($fp,'<?php exit(); ?>');
			}
			
			$xml_file = 'cache/atom/'.$name.'.xml';
			
			if(file_exists($xml_file))
			{
				$fp = fopen($xml_file,'r');
				
				$stat = fstat($fp);
				
				//Refreshing the cache after 30 minutes
				$limit = time()-(60*30);
				if($stat['mtime']<$limit)
				{
					fclose($fp);
					$this->resyncCache($xml_file);
				}
				else
				{
					while($line = fgets($fp))
					{
						$this->xml .= $line;
					}	
					fclose($fp);
					
				}
			}
			else
			{
				$this->resyncCache($xml_file);
			}
			
		}
		else
		{
			$fp = fopen($url,'r');
		
			while($line = fgets($fp))
			{
				$this->xml .= $line;
			}
		}
	}
	
	private function resyncCache($xml_file)
	{
		$this->xml = '';
		$fp_url = fopen($this->url,'r');
		$fp = fopen($xml_file,'w');
		
		while($line = fgets($fp_url))
		{
			$this->xml .= $line;
			fwrite($fp,$line);
		}
		
		fclose($fp);
		fclose($fp_url);
		
		chmod($xml_file,0777);
		
	}
	
	public function parseFeed()
	{
		preg_match_all('#<entry>(.+)<updated>(.+)</updated>(.+)<link (.+)href="(.+)"(.+)>(.+)<title type="html">(.+)</title>(.+)<author>(.+)<name>(.+)</name>(.+)</author>(.+)</entry>#isU',$this->xml,$matches);
		
		$dates = $matches[2];
		$links = $matches[5];
		$titles = $matches[8];
		$authors = $matches[11];
		
		$nb_entry = count($dates);
		
		for($i = 0; $i<$nb_entry; $i++)
		{
			$this->entry[$i] = new FeedEntry();
			$this->entry[$i]->link = $links[$i];
			$this->entry[$i]->title = $titles[$i];
			$this->entry[$i]->updated = $dates[$i];
			$this->entry[$i]->author = $authors[$i];
		}
	}
	
	public function count()
	{
		return count($this->entry);
	}
}


?>